using cmd;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public abstract class FishTool : Singleton<FishTool>
{
    [Header("基础碰撞设置")]
    [Tooltip("碰撞冷却时间")]
    [SerializeField] protected float collisionCooldown = 0.5f;
    [Tooltip("自动禁用碰撞组件")]
    [SerializeField] protected bool autoDisableCollider = true;

    [Header("事件系统")]
    protected UnityEvent<FishSaveData> OnFishCaught;

    private BoxCollider2D _hookCollider;
    public LayerMask ignoreLayers; 

    protected virtual void Start()
    {
        _hookCollider = GetComponent<BoxCollider2D>();
        if (_hookCollider == null)
        {
            Debug.LogError("未找到碰撞体组件", this);
        }
        else if (!_hookCollider.isTrigger)
        {
            Debug.LogWarning("建议将碰撞体设为触发器（Is Trigger）以获得更稳定的检测效果", _hookCollider);
        }
        
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if ((ignoreLayers.value & (1 << other.gameObject.layer)) != 0)
        {
            return; // 如果是忽略的 Layer，直接返回
        }
        //根据触碰tag，区分钓鱼时间
        //钓鱼结束 触发内容：钩子回收、鱼肉销毁、碰撞关闭=开启冷却、数据存储
        //钓鱼开始 触发内容：碰撞开启
        if (other.TryGetComponent<Fish>(out var fishMonobehavior))
        {
            HandleValidCollision(fishMonobehavior);
            Destroy(other.gameObject);
        }
        else
        {
            Debug.LogWarning($"检测到未携带FishData组件的鱼：{other.name}", other.gameObject);
        }
        JinYong();
    }
    protected abstract void HandleValidCollision(Fish other);
    public abstract string GetToolName();
    public void JinYong()
    {
        _hookCollider.enabled = false;
        Body.Instance.StartReset();
        LevelManager.Instance.Check();
    }

    public void KaiQi()
    {
        _hookCollider.enabled = true;
     }
}